package com.hjh.netty.elect.demo4;

import org.springframework.util.StringUtils;

/**
 *  字符串转换工具
 * @author pyz
 *
 */
public class StringDataConverUtils {

	/**
	 * 字符串不够长度，前面补0
	 * @param data 字符串
	 * @param totalLength 总长度
	 * @return
	 */
	public static String dataAutoLength(String data, int totalLength) {
		if(!StringUtils.hasText(data)) {
			return data;
		}
		if(data.length() < totalLength) {
			StringBuilder addStr = new StringBuilder();
			for(int i = 0; i < (totalLength - data.length()); i++) {
				addStr.append("0");
			}
			data = addStr.toString() + data;
		}
		return data;
	}
	
	/**
	 * 数值不够长度，前面补0
	 * @param num 数值
	 * @param totalLength
	 * @return
	 */
	public static String dataAutoLength(int num, int totalLength) {
		return String.format("%0" + totalLength + "d", num);
	}
	
	
	public static void main(String[] args) {
		System.out.println(dataAutoLength("10086", 8));
	}
}
